home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / Commodities / Common / cx.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  11.0 KB  |  344 lines

  1.  
  2.    /***********************************************************************
  3.    *                                                                      *
  4.    *                            COPYRIGHTS                                *
  5.    *                                                                      *
  6.    *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.    *
  7.    *                                                                      *
  8.    ***********************************************************************/
  9.  
  10. /*
  11.  * cx.c -- Commodities interface
  12.  *
  13.  * This module handles all the command messages from commodities.library.
  14.  * Commands such as HIDE SHOW ENABLE DISABLE KILL are sent to commidities
  15.  * from the Exchange program and are processed here.
  16.  */
  17.  
  18. #include "local.h"
  19.  
  20. CxObj *broker = NULL;            /* Our broker */
  21.  
  22. /* a little global for showing the user the hotkey   */
  23. char   hotkeybuff[ 257 ];
  24.  
  25. struct NewBroker mynb = {
  26.    NB_VERSION,                        /* Library needs to know version */
  27.    COM_NAME,                          /* broker internal name          */
  28.    COM_TITLE,                         /* commodity title               */
  29.    COM_DESCR,                         /* description                   */
  30.    NBU_NOTIFY | NBU_UNIQUE,           /* We want to be the only broker */
  31.                                       /* with this name and we want to */
  32.                                       /* be notified of any attempts   */
  33.                                       /* to add a commodity with the   */
  34.                                       /* same name                     */
  35.    0,                                 /* flags                         */
  36.    0,                                 /* default priority              */
  37.    NULL,                              /* port, will fill in            */
  38.    0                                  /* channel (reserved)            */
  39. };
  40.  
  41. /****i* Blank.ld/handleCxMsg() ******************************************
  42. *
  43. *   NAME
  44. *        handleCxMsg -- Handle incoming commodities messages.
  45. *
  46. *   SYNOPSIS
  47. *        handleCxMsg(msg)
  48. *
  49. *        VOID handleCxMsg(Struct Message *msg);
  50. *
  51. *   FUNCTION
  52. *        This function handles commodities messages sent to the brokers
  53. *        message port. This is were the standard commodities features
  54. *        such as Enable/Disable Show/Hide Quit and HotKey PopUp are
  55. *        handled. If the  message is not for the standard handler then the
  56. *        function handleCustomCXMsg() is called to handle application
  57. *        specific IEVENTS otherwise handleCustomCXCommand() is called
  58. *        to handle application specific commodities command messages.
  59. *
  60. *   INPUTS
  61. *        msg = A commodities message;
  62. *
  63. *   RESULT
  64. *        Either the standard commodities function is performed or the
  65. *        custom handler is called.
  66. *
  67. *   EXAMPLE
  68. *
  69. *   NOTES
  70. *
  71. *   BUGS
  72. *
  73. *   SEE ALSO
  74. *
  75. *****************************************************************************
  76. *
  77. */
  78.  
  79. VOID handleCxMsg(struct Message *msg)
  80. {
  81.    ULONG   msgid;
  82.    ULONG   msgtype;
  83.  
  84.    msgid   = CxMsgID( msg );
  85.    msgtype = CxMsgType( msg );
  86.  
  87.    D1( printf("cx.c: handleCxMsg() enter\n") );
  88.  
  89.    ReplyMsg(msg);
  90.  
  91.    switch(msgtype)
  92.    {
  93.       case CXM_IEVENT:
  94.          D1( printf("cx.c: handleCxMsg(CXM_IEVENT)\n") );
  95.          switch(msgid)
  96.          {
  97.             W(
  98.             case POP_KEY_ID:
  99.                   D1( printf("cx.c: handleCxMsg(POP_KEY_ID)\n") );
  100.                   setupWindow();
  101.                   break;
  102.             )
  103.             default:
  104.                   D1( printf("cx.c: handleCxMsg(Custom Message)\n") );
  105.                   handleCustomCXMsg(msgid);
  106.                   break;
  107.          }
  108.          break;
  109.       case CXM_COMMAND:
  110.          D1( printf("cx.c: handleCxMsg(CXM_COMMAND)\n") );
  111.          switch(msgid)
  112.          {
  113.             case CXCMD_DISABLE:
  114.                D1( printf("cx.c: handleCxMsg(CXCMD_DISABLE)\n") );
  115.                ActivateCxObj(broker,0L);
  116.                break;
  117.             case CXCMD_ENABLE:
  118.                D1( printf("cx.c: handleCxMsg(CXCMD_ENABLE)\n") );
  119.                ActivateCxObj(broker,1L);
  120.                break;
  121.             case CXCMD_APPEAR:   /* Time to pop up the window         */
  122.             case CXCMD_UNIQUE:   /* Someone has tried to run us again */
  123.                D1( printf("cx.c: handleCxMsg(CXCMD_APPEAR|CXCMD_UNIQUE)\n") );
  124.                /* If someone tries to run us a second time the second copy
  125.                 * of the program will fail and we will be sent a
  126.                 * CXCMD_UNIQUE message. If we support a window then we
  127.                 * Make our window appear since that is what the user wanted.
  128.                 * If we do not support a window then we kill the currently
  129.                 * running version 'this one' so that things like autopoint
  130.                 * can be toggled on/off by running them a second time.
  131.                 */
  132.                if(WINDOW)
  133.                {
  134.                   W( setupWindow(); )
  135.                } else {
  136.                   terminate();
  137.                }
  138.                break;            /* the window                        */
  139.             case CXCMD_DISAPPEAR:
  140.                D1( printf("cx.c: handleCxMsg(CXCMD_DISAPPEAR)\n") );
  141.                W(
  142.                   shutdownWindow();
  143.                )
  144.                break;
  145.             case CXCMD_KILL:
  146.                D1( printf("cx.c: handleCxMsg(CXCMD_KILL)\n") );
  147.                terminate();
  148.                break;
  149.             default:
  150.                D1( printf("cx.c: handleCxMsg(Custom Command)\n") );
  151.                handleCustomCXCommand(msgid);
  152.                break;
  153.          }     /* end switch(command) */
  154.          break;
  155.    }     /* end switch(msgtype) */
  156. }
  157. /****i* Blank.ld/setupCX() ******************************************
  158. *
  159. *   NAME
  160. *        setupCX -- Initialize commodities.library specific features.
  161. *
  162. *   SYNOPSIS
  163. *        result = setupCX(ttypes)
  164. *
  165. *        BOOL setupCX(char **ttypes);
  166. *
  167. *   FUNCTION
  168. *        This function performs all the commodities.library specific
  169. *        setup required for a standard commodity. It sets up the brokers
  170. *        message port, and priority. And sets certain flags in the
  171. *        broker structure so the exchange program will know what
  172. *        features this broker supports. If the commodity supports a window
  173. *        the windows hotkey is added to the broker and then the function
  174. *        setupCustomCX() is called to setup the application specific
  175. *        commodities objects. If all this goes well the broker is activated
  176. *        and the function returns TRUE.
  177. *
  178. *   INPUTS
  179. *        ttypes - NULL terminated Argument array containing this
  180. *        applications TOOLTYPES strings.
  181. *
  182. *   RESULT
  183. *        Returns TRUE if all went OK else returns FALSE.
  184. *
  185. *   EXAMPLE
  186. *        if(setupCX(ttypes))
  187. *        {
  188. *           printf("Commodities successfully initialized.\n");
  189. *        } else {
  190. *           printf("Commodities initialization error!\n");
  191. *        }
  192. *
  193. *   NOTES
  194. *        This function can be called at anytime to reinitialize the
  195. *        commodities code from a new set of arguments.
  196. *
  197. *   BUGS
  198. *
  199. *   SEE ALSO
  200. *        setupCustomCX();
  201. *        shutdownCX();
  202. *        shutodwnCustomCX();
  203. *
  204. *****************************************************************************
  205. *
  206. */
  207. BOOL setupCX(char **ttypes )
  208. {
  209.    LONG   error;
  210.    W( char   *str; )
  211.  
  212.    D1( printf("cx.c: setupCX() enter\n"); )
  213.  
  214.    shutdownCX();                          /* remove what was and create */
  215.                                           /* everything from scratch    */
  216.  
  217.    cxport=CreatePort(mynb.nb_Name,0L);    /* Create Message port        */
  218.    if( ! cxport )
  219.    {
  220.       D1( printf("cx.c: setupCX() Could not create message port\n"); )
  221.       return(FALSE);
  222.    }
  223.    D1( printf("cx.c: setupCX() cxport=0x%lx\n",cxport); )
  224.  
  225.    cxsigflag = 1L << cxport->mp_SigBit;   /* Create signal mask for Wait*/
  226.  
  227.    /* Set the brokers priority from the TOOLTYPES or from default if no */
  228.    /* TOOLTYPES are available. Set the brokers Message port.            */
  229.    mynb.nb_Pri  = ArgInt( ttypes, PRIORITY_TOOL_TYPE, CX_DEFAULT_PRIORITY );
  230.    mynb.nb_Port = cxport;
  231.  
  232.    D1( printf("cx.c: setupCX() mynb.nb_pri=0x%lx\n",mynb.nb_Pri); )
  233.  
  234.    /* If this commodity supports a window then set the SHOW/HIDE flag   */
  235.    /* so the Exchange controller can ghost its gadgets appropriately    */
  236.    W( mynb.nb_Flags |= COF_SHOW_HIDE; )
  237.  
  238.    /* Attempt to create our broker */
  239.    if ( ! ( broker = CxBroker( &mynb, NULL ) ) )
  240.    {
  241.       D1( printf("cx.c: setupCX() could not create broker\n"); )
  242.       shutdownCX();
  243.       return(FALSE);
  244.    }
  245.    D1( printf("cx.c: setupCX() broker=0x%lx\n",broker); )
  246.  
  247.    /* If this commodity supports a window then add its HotKey now       */
  248.    W(
  249.       /* install a hotkey for popping up window   */
  250.       AttachCxObj(broker,
  251.                HotKey(str=ArgString(ttypes,POPKEY_TOOL_TYPE,CX_DEFAULT_POP_KEY),
  252.                cxport,POP_KEY_ID) );
  253.       strncpy(hotkeybuff,str,sizeof(hotkeybuff)-1);
  254.    )
  255.  
  256.    /* Setup all application specific commodities objects */
  257.    if( ! setupCustomCX() )
  258.    {
  259.       D1( printf("cx.c: setupCX() setupCustomCX failed\n"); )
  260.       shutdownCX();
  261.       return(FALSE);
  262.    }
  263.  
  264.    /* Check for accumulated error */
  265.    if ( error = CxObjError( broker ) )
  266.    {
  267.       D1( printf("cx.c: setupCX() accumulated broker error %ld\n",error) );
  268.       shutdownCX();
  269.       return (FALSE);
  270.    }
  271.  
  272.    /* All went well so activate our broker */
  273.    ActivateCxObj(broker,1L);
  274.  
  275.    D1( printf("cx.c: setupCX() returns TRUE") );
  276.    return (TRUE);
  277. }
  278.  
  279. /****i* Blank.ld/shutdownCX() ******************************************
  280. *
  281. *   NAME
  282. *        shutdownCX -- Cleanup all commodities brokers and handlers.
  283. *
  284. *   SYNOPSIS
  285. *        shutdownCX()
  286. *
  287. *        VOID shutdownCX(VOID);
  288. *
  289. *   FUNCTION
  290. *        Shuts down and cleans up all variables and data used for supporting
  291. *        the commodities.library side of this commodity. This function
  292. *        MUST be set up so that it can be called regardless of the current
  293. *        state of the program. This function handles all the standard
  294. *        cleanup and calls shutdownCustomCX(); to cleanup the application
  295. *        specific code.
  296. *
  297. *   INPUTS
  298. *        None.
  299. *
  300. *   RESULT
  301. *        The commodities specific code is cleaned up and made ready for
  302. *        a terminate(); or a call to setupCX();.
  303. *
  304. *   EXAMPLE
  305. *
  306. *   NOTES
  307. *        The first thing that setupCX() does is call this routine. Therefore
  308. *        this function must work even before your commodity has been
  309. *        initialized.
  310. *
  311. *   BUGS
  312. *
  313. *   SEE ALSO
  314. *        setupCX();
  315. *        setupCustomCX();
  316. *        shutdownCustomCX();
  317. *
  318. *****************************************************************************
  319. *
  320. */
  321. VOID shutdownCX()
  322. {
  323.    struct Message   *msg;
  324.  
  325.    D1( printf("cx.c: shutdownCX() enter broker now: %lx\n", broker ) );
  326.    shutdownCustomCX();
  327.  
  328.    if(cxport)
  329.    {
  330.       D1( printf("cx.c: shutdownCX() Deleting all objects\n") );
  331.       DeleteCxObjAll(broker);      /* safe, even if NULL   */
  332.  
  333.       /* now that messages are shut off, clear port   */
  334.       while(msg=GetMsg(cxport)) ReplyMsg(msg);
  335.       DeletePort(cxport);
  336.  
  337.       cxport    = NULL;
  338.       cxsigflag = 0;
  339.       broker    = NULL;
  340.    }
  341.    D1( printf("cx.c: shutdownCX() returns\n") );
  342. }
  343.  
  344.